package co.fitstart.api.web.controller.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.api.service.ProductService;
import co.fitstart.entity.product.Product;
import co.fitstart.entity.product.ProductCategory;
import co.fitstart.entity.product.ProductType;

/**
 *
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/v1")
public class ProductApiController {

    @Autowired
    private ProductService productService;

    /**
     * List all products
     * 
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public String listHotProducts() {
        List<Product> products = productService.listAvailableProduct();
        Result result = new Result().success().data("products", products);
        return JsonUtils.toJsonString(result);
    }

    /**
     * Get product types
     * @return
     */
    @ResponseBody
    @Cacheable("productType")
    @RequestMapping("/product-types")
    public String listProductTypes() {
        ProductType[] types = ProductType.values();
        List<Map<String, String>> typeMaps = new ArrayList<>(types.length);
        for (ProductType type : types) {
            Map<String, String> typeMap = new HashMap<>();
            typeMap.put("name", type.getAlias().toUpperCase());
            typeMap.put("description", type.getDescription());
            typeMap.put("style", type.getCss());
            typeMaps.add(typeMap);
        }
        return JsonUtils.toJsonString(new Result().success().data("productTypes", typeMaps));
    }

    /**
     * Get product types
     * @return
     */
    @Cacheable("productCategory")
    @RequestMapping("/product-categories")
    public @ResponseBody String listProductCategories() {
        Result result = new Result();
        List<ProductCategory> productCategories = productService.listProductCategories();
        Map<String, List<ProductCategory>> categoryMap = CollectionUtils.constructMapValuesFromList(productCategories, "type");
        return JsonUtils.toJsonString(result.success().data("productCategories", categoryMap));
    }
    
    /**
     * 
     * @param productId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/products/{productId}", method = RequestMethod.GET)
    public String productDetail(@PathVariable("productId") Long productId) {
        Result result = new Result();
        Product product = productService.loadProduct(productId);
        return JsonUtils.toJsonString(result.success().data("product", product).data("description", product.getDescription()));
    }

}
